﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Security.Cryptography;

namespace RunCoachServer
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    public class RunCoachService : IRunCoachService
    {
        private static runcoachEntities _entities;
        private String _username;

        RunCoachService()
        {
            try
            {
                _entities = new runcoachEntities();
                _username = "";
            }
            catch (Exception e)
            {
                Console.WriteLine("Konstruktor hiba: " + e.ToString());
            }
        }
        
        public Boolean Login(String username, String password)
        {
            using (MD5 md5Hash = MD5.Create())
            {
                password = GetMd5Hash(md5Hash, password);

                try
                {
                    users akt_user = _entities.users.FirstOrDefault(user => user.username == username && user.password == password);
                    _username = username;

                    return (akt_user != null);

                }
                catch(Exception e)
                {
                    Console.WriteLine("Login hiba: " + e.ToString());
                    return false;
                }
            }
        }

        public void Logout()
        {
            _username = "";
        }

        public Boolean RegisterUser(String username, String realname, String password)
        {
            using (MD5 md5Hash = MD5.Create())
            {
                password = GetMd5Hash(md5Hash, password);

                try
                {
                    users akt_user = _entities.users.FirstOrDefault(user => user.username == username || user.real_name == realname);

                    if (akt_user != null)
                    {
                        return false;
                    }

                    users new_user = new users();
                    new_user.username = username;
                    new_user.real_name = realname;
                    new_user.password = password;

                    _entities.users.AddObject(new_user);

                    return (_entities.SaveChanges() == 1);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Regisztracio hiba: " + e.ToString());
                    return false;
                }
            }
        }

        public Boolean getDataFromMobile(Array datas)
        {

            return true;
        }

        public Boolean deleteRunData(Int32 id)
        {

            return true;
        }

        private static string GetMd5Hash(MD5 md5Hash, string input)
        {
            // Convert the input string to a byte array and compute the hash. 
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data and format each one as a hexadecimal string. 
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string. 
            return sBuilder.ToString();
        }
    }
}
